/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_place.h"

#include "adventure_map.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "compressed_filter.h"
#include "game_time.h"

#include "saved_game_header.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_place::t_adventure_event_place ( ) 
: t_adventure_event_base ( k_place_event ) 
{ 	
	m_event_buffer	= new t_memory_buffer_counted ( k_memory_event_buffer_size ) ;
}

t_adventure_event_place::t_adventure_event_place ( t_adventure_object* adv_object ) 
: t_adventure_event_base ( k_place_event ) 
{ 
	m_global_id		= adv_object->get_global_id();
	m_event_buffer	= t_adventure_event_functions::create_event_buffer(adv_object);
}

void t_adventure_event_place::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{
	int size = m_event_buffer->size();

	t_adventure_event_functions::create_and_place_adv_object( map, m_event_buffer, header );

	m_time_end = get_time() + k_event_lifespan;
	
	return;
}

void t_adventure_event_place::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{
	map->remove_object(m_global_id);
}

bool t_adventure_event_place::read( std::streambuf & buffer , int version )
{
	m_event_buffer->clear_buffer();

	int event_buffer_flag  = 	get<t_uint32> ( buffer );

	if (event_buffer_flag != 0)
	{
		m_event_buffer->read( buffer );
	}

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_place::write( std::streambuf & buffer )
{
	if (m_event_buffer.get() != 0)
	{
		put<t_uint32> (buffer, 1);

		m_event_buffer->write( buffer );
	}
	else
		put<t_uint32> (buffer, 0);
	
	return t_adventure_event_base::write( buffer );
}
